package com.handinfo.android.core.object;

import android.net.TrafficStats;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.core.graphics.DWScreen;
import com.handinfo.android.utils.Tools;

public abstract class AbstractGameHandler implements IGameHandler
{
	protected boolean m_running = true;
	protected boolean m_pause = false;

	protected static long Fps_Start = 0;
	protected static long Fps_End = 0;
	public static int Fps = 0;
	public static int Draw_Time = 0;

	public long m_frameCounter;

	private int m_frameInterval = DWGameManager.FRAME_INTERVAL;

	protected long m_trafficAmount;
	protected long m_trafficPerSecond;
	protected long m_trafficRecordTime;

	public boolean isRunning()
	{
		return m_running;
	}

	public void setRunning(boolean mRunning)
	{
		this.m_running = mRunning;
	}

	public void setPause(boolean mPause)
	{
		this.m_pause = mPause;
	}

	public void resume()
	{
	}

	public void pause()
	{
	}

	public void stop()
	{
	}
	
	public void start()
	{
	}
	
	public void destroy()
	{
		m_running = false;
	}

	public int getFrameInterval()
	{
		return m_frameInterval;

	}

	public void setFrameInterval(int frameInterval)
	{
		m_frameInterval = frameInterval;
	}

	public void run() throws Exception
	{
		long tick = 0;
		try {
			init();
		}
		catch (Exception e) {
			Tools.debug(e);
		}
		DWGraphics g = DWScreen.Instance.getGraphics();
		while (m_running) {
			tick = System.currentTimeMillis() + m_frameInterval;
			
			if (DWGraphics.DEBUG_GRAPHICS) {
				DWGraphics.DrawBitmapSquareAmount = 0;
				DWGraphics.DrawBitmapTimes = 0;
				DWGraphics.DrawBitmapCount = 0;
			}

			while (m_pause) {
				Thread.yield();
			}

			Fps_Start = System.currentTimeMillis();

//			DWGameManager.getInstance().checkInputMethod();
			try {
				logic();
			}
			catch (Exception e) {
				Tools.debug(e);
			}

			try {
				if (DWGameManager.getInstance().m_isPainting) {
					paint(g);
				}
			}
			catch (Exception e) {
				Tools.debug(e);
			}
			
			adjustLight();

			DWGameManager.getInstance().flush();
			DWGameManager.getInstance().keepConnectionAlive();
			DWGameManager.getInstance().networkisnotgood();
			
			Fps_End = System.currentTimeMillis();
			Draw_Time = (int) (Fps_End - Fps_Start);
			Fps = 1000 / (Draw_Time + 1);

			while (tick > System.currentTimeMillis()) {
				Thread.yield();
			}

			if (m_frameCounter % 50 == 0) {
				m_trafficAmount = TrafficStats.getUidRxBytes(android.os.Process.myUid())
						+ TrafficStats.getUidTxBytes(android.os.Process.myUid()) - DWGameManager.s_traffic_amount_last;
			}
			m_frameCounter++;

		}
		quit();
	}

	public void paintFPS(DWGraphics g)
	{
		if (Tools.DEBUG) {
			g.setClip(0, 0, DWGameManager.Screen_Width, DWGameManager.Screen_Height);
			g.setColor(0xff, 0xff, 0xff, 0xff);
			
			if (DWGraphics.DEBUG_GRAPHICS) {
				g.drawString("DrawBitmapSquare: " + DWGraphics.DrawBitmapSquareAmount, 0xff0000ff, 10,
						DWGameManager.Screen_Height - 150, DWGraphics.TOP
								| DWGraphics.LEFT);
				g.drawString("DrawBitmapCount: " + DWGraphics.DrawBitmapCount, 0xff0000ff, 10,
						DWGameManager.Screen_Height - 125, DWGraphics.TOP
								| DWGraphics.LEFT);
				g.drawString("DrawBitmapTimes: " + DWGraphics.DrawBitmapTimes / 1000000, 0xff0000ff, 10,
						DWGameManager.Screen_Height - 100, DWGraphics.TOP
								| DWGraphics.LEFT);
			}
			g.drawString("   流量   : " + m_trafficAmount / 1000 + "K", 0xff0000ff, 10, DWGameManager.Screen_Height - 75,
					DWGraphics.TOP | DWGraphics.LEFT);
			g.drawString("DrawTime: " + Draw_Time, 0xff0000ff, 10, DWGameManager.Screen_Height - 50, DWGraphics.TOP
					| DWGraphics.LEFT);
			g.drawString("   FPS  : " + Fps, 0xff0000ff, 10, DWGameManager.Screen_Height - 25, DWGraphics.TOP
					| DWGraphics.LEFT);
		}
	}
	
	
	private int m_last_light_value;
	private void adjustLight()
	{
		try {
			if (m_last_light_value == DWGameManager.s_setting_light)
				return;
			m_last_light_value = DWGameManager.s_setting_light;
			if (DWGameManager.getInstance().getCurrentActivity() == null)
				return;
			DWGameManager.getInstance().getCurrentActivity().mHandler.post(new Runnable() {
				public void run()
				{
					DWGameManager.getInstance().getCurrentActivity().setScreenBrightness(m_last_light_value / 100.0f);
				}
			});
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}

	/**
	 * 初始化
	 */
	public abstract void init();

	/**
	 * 销毁
	 */
	public abstract void quit();

}
